package com.dp.appi.service.impl;

import com.dp.appi.dao.OauthDao;
import com.dp.appi.exception.OauthRuntimeException;
import com.dp.appi.service.OauthService;
import com.dp.appi.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by xiaoji on 2016/4/20.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class OauthServiceImpl implements OauthService {
    @Autowired
    private OauthDao oauthDao;

    /**
     * 注册
     *
     * @param username    用户名
     * @param mobilePhone 手机号码
     * @param pwd
     */
    @Override
    public void insertUser(String username, String mobilePhone, String pwd) {
        //判断账号和手机号码是否存在
        boolean boo1 = oauthDao.isExsitUsername(username);
        boolean boo2 = oauthDao.isExsitUsername(mobilePhone);
        if(boo1){
            throw new OauthRuntimeException(SYS.OA001.name());
        }
        if(boo2){
            throw new OauthRuntimeException(SYS.OA002.name());
        }
        oauthDao.insertUser(username,mobilePhone,pwd);
    }

    /**
     * 根据账号获取用户对象
     *
     * @param username 账号
     * @return
     */
    @Override
    public Map<String, Object> getUser(String username) {
        return oauthDao.getUser(username);
    }

    /**
     * 重设密码
     *
     * @param id     用户id
     * @param newpwd 新密码
     */
    @Override
    public void resetPwd(String id, String newpwd) {
        oauthDao.resetPwd(id,newpwd);
    }

    /**
     * 获取密保问题
     *
     * @param id 用户id
     * @return
     */
    @Override
    public Map<String, Object> getSecurityProblem(String id) {
        return oauthDao.getSecurityProblem(id);
    }

    /**
     * 修改密码
     *
     * @param id  用户id
     * @param pwd 密码
     */
    @Override
    public void updatePwd(String id, String pwd) {
        oauthDao.updatePwd(id,pwd);
    }

    /**
     * 设置密保答案
     *
     * @param id       登录id
     * @param problem1 问题1
     * @param answer1  答案1
     * @param problem2 问题2
     * @param answer2  答案2
     */
    @Override
    public void setSecurityProblem(String id, String problem1, String answer1, String problem2, String answer2) {
        oauthDao.setSecurityProblem(id,problem1,answer1,problem2,answer2);
    }
}
